// pages/user/order/order.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    status:10,
    order:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this
    _this.initList(_this.data.status)
  },
  /**
   * 列表
   */
  initList:function(params){
    let that=this
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/order/list?access_token=' + accessToken + '&status=' + params,
        success: res => {
          if (res.data.status === 1) {
            that.setData({
              order:res.data.data.to_order
            })
          }
          wx.hideLoading()
        }
      })
    })
  },
  
  /**
   * tab
   */
  tab: function(e) {
    let that = this,
        tabIndex = e.currentTarget.dataset.index
    this.setData({
      active: tabIndex,
      status: e.currentTarget.dataset.status
    })
    that.initList(that.data.status)
  },

  /**
   * del
   */
  delOrder: function(e) {
    let that=this
    wx.showModal({
      title: '提示',
      content: '是否删除订单',
      success: res => {
        if(res.confirm) {
          app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
            wx.request({
              url: app.globalData.hostAPI + '/v1/order/cancel?access_token=' + accessToken + '&out_trade_no=' + e.currentTarget.dataset.id,
              success: res => {
                if (res.data.status===1){
                  that.initList(that.data.status)
                }
              }
            })
          })
        }
      }
    })
  },

  sign: function(e) {
    let that = this
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/order/signin?access_token=' + accessToken,
        data: {
          out_trade_no: e.currentTarget.dataset.id
        },
        success: res => {
          if (res.data.status === 1) {
            that.initList(1)
          }
        }
      })
    })
  },

  /**
   * finish
   */
  finish: function(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否结束订单',
      success: res => {
        if(res.confirm) {
          app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
            wx.request({
              url: app.globalData.hostAPI + '/v1/order/finished?access_token=' + accessToken + '&out_trade_no=' + e.currentTarget.dataset.id,
              success: res => {
                console.log(res)
                if (res.data.status === 1) {
                  that.initList(2)
                  wx.showToast({
                    title: '结束订单',
                  })
                }
              }
            })
          })
        }
      }
    })
  },
 
  /**
   * rate
   */
  rateOrder: function(e) {
    let order_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../../rate/rate?order_id=' + order_id,
    })
  },

  /**
   * makephone
   */
  makephone: function(e) {
    let phone = e.currentTarget.dataset.tel
    if(phone != null) {
      wx.makePhoneCall({
        phoneNumber: phone,
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '预留号码无效',
      })
    }
  },

  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    that.initList(that.data.status)
    wx.hideNavigationBarLoading()
    wx.stopPullDownRefresh()
  },
})